package com.group01.community.controller;


import com.group01.community.entity.ZyComment;
import com.group01.community.entity.ZyCommunityInteraction;
import com.group01.community.entity.vo.ZyCommentUserVo;
import com.group01.community.entity.vo.ZyCommentVo;
import com.group01.community.entity.vo.ZyCommunityInteractionVO;
import com.group01.community.service.ZyCommentService;
import com.group01.community.utils.ResponseResult;
import com.group01.community.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 评论表 前端控制器
 * </p>
 *
 * @author xuxu
 * @since 2022-07-04
 */
@RestController
@RequestMapping("/zyComment")
public class ZyCommentController {

    @Autowired
    private ZyCommentService zyCommentService;

    /**
     * 获取所有的评论集合
     * @param interactionId   社区互动Id
     * @return                 评论的集合
     */
    @GetMapping("list/{interactionId}")
    public ResponseResult<List<ZyCommentVo>> getAllComment(@PathVariable Long interactionId){
        List<ZyCommentVo> list = zyCommentService.getAllCommentByInteractionId(interactionId);
        return ResponseResult.ok(list);
    }

    /**
     * 通过评论Id删除评论
     * @param zcCommentId       评论Id
     * @return                  删除后返回ok
     */
    @DeleteMapping("delete/{zcCommentId}")
    public ResponseResult<String> deleteComment(@PathVariable Long zcCommentId){
        zyCommentService.deleteByCommentIdAfter(zcCommentId);
        return ResponseResult.ok("ok");
    }

    /**
     * 删除社区互动及以下评论
     * @param interactionId     社区互动Id
     * @return                  ok
     */
    @DeleteMapping("del/{interactionId}")
    public ResponseResult<String> deleteInteractionByInteractionId(@PathVariable Long interactionId){
        if (("ok").equals(zyCommentService.deleteInteractionByInteractionId(interactionId))){
            return ResponseResult.ok("ok");
        }
        return ResponseResult.ok("bad");
    }

    /**
     *  添加评论
     * @param zyCommunityInteraction 对象
     * @return  ok
     */
    @PostMapping("add")
    public ResponseResult<String> add(ZyCommunityInteractionVO zyCommunityInteraction){
        ZyComment zyComment = new ZyComment();
        zyComment.setCreateTime(LocalDateTime.now());
        zyComment.setCommentId(UUIDUtils.getId());
        zyComment.setContent(zyCommunityInteraction.getContent());
        zyComment.setCreateBy(zyCommunityInteraction.getCreateBy());
        zyComment.setParentId(zyCommunityInteraction.getInteractionId());
        zyComment.setInteractionId(zyCommunityInteraction.getInteractionId());
        zyComment.setUserId(zyCommunityInteraction.getUserId());
        zyCommentService.save(zyComment);
        return ResponseResult.ok("ok");
    }

    /**
     * 查询回复评论的昵称
     * @param interactionId     社区互动Id
     * @return                  comment集合
     */
    @GetMapping("getAllCommentAndOwner/{interactionId}")
    public ResponseResult<List<ZyCommentUserVo>> getAllCommentAndOwner(@PathVariable Long interactionId){
        return ResponseResult.ok(zyCommentService.getAllCommentAndOwner(interactionId));
    }

}

